home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / scripts / createdb.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1992-08-27  |  6.9 KB  |  308 lines

  1. #!/bin/sh
  2. # ----------------------------------------------------------------
  3. #   FILE
  4. #    createdb    create a postgres database
  5. #
  6. #   DESCRIPTION
  7. #    this program feeds the proper input to the ``postgres'' program
  8. #    to create a postgres database and register it in the
  9. #    shared ``pg_database'' database.
  10. #
  11. #   NOTES
  12. #    currently the postgres does not do any locking on pg_database
  13. #    so it is possible that pg_database could be corrupted if we
  14. #    create a new database while a postgres/postmaster session is
  15. #    in progress.  Someday locking code will have to be added 
  16. #    to this script.
  17. #
  18. #   IDENTIFICATION
  19. #     $Header: /private/postgres/src/scripts/RCS/createdb.sh,v 1.3 1992/02/26 01:54:30 hong Exp $
  20. # ----------------------------------------------------------------
  21.  
  22. CMDNAME=`basename $0`
  23.  
  24. # ----------------
  25. #     check arguments:
  26. #         -d indicates debug mode.
  27. #        -n means don't clean up on error (so your cores don't go away)
  28. # ----------------
  29. debug=0
  30. noclean=0
  31.  
  32. for ARG
  33. do
  34.     case "$ARG" in
  35.     -d)    debug=1; echo "$CMDNAME: debug mode on";;
  36.     -n)    noclean=1; echo "$CMDNAME: noclean mode on";;
  37.     *)    FILES="$FILES $ARG";;
  38.     esac
  39. done
  40.  
  41. # ----------------
  42. #     if the debug flag is set, then 
  43. # ----------------
  44. if (test "$debug" -eq 1)
  45. then
  46.     BACKENDARGS="-boot -COd -ami"
  47. else
  48.     BACKENDARGS="-boot -COQ -ami"
  49. fi
  50.  
  51. # ----------------
  52. #     if no files are specified, assume the username.
  53. # ----------------
  54. if (test -z "$FILES")
  55. then
  56.     FILES=$USER
  57.     if (test "$debug" -eq 1)
  58.     then
  59.         echo "$CMDNAME: no database name specified, assuming: $FILES"
  60.     fi
  61. fi
  62.  
  63. # ----------------
  64. #     check POSTGRESHOME
  65. # ----------------
  66. if (test -z "$POSTGRESHOME")
  67. then
  68.     echo "$CMDNAME: POSTGRESHOME not set."
  69.     exit 1
  70. else
  71.     PG=`echo $POSTGRESHOME | sed -e 's/\([^:]*\):.*/\1/'`
  72.     PGS=`echo $POSTGRESHOME | sed -e 's/:[0-9]$//' | sed -e 's/:/ /g'`
  73. fi
  74.  
  75. # ----------------
  76. #     check POSTGRESTREE
  77. # ----------------
  78. if (test -z "$POSTGRESTREE")
  79. then
  80.     TREE=$PG
  81. else
  82.     TREE=$POSTGRESTREE
  83. fi
  84.  
  85. # ----------------
  86. #     check POSTGRESBIN
  87. # ----------------
  88. if (test -z "$POSTGRESBIN")
  89. then
  90.     PGBIN=$PG/bin
  91. else
  92.     PGBIN=$POSTGRESBIN
  93. fi
  94.  
  95. # ----------------
  96. #     check POSTGRESTEMP
  97. # ----------------
  98. if (test -z "$POSTGRESTEMP")
  99. then
  100.     PGTEMP=$PG/files
  101. else
  102.     PGTEMP=$POSTGRESTEMP
  103. fi
  104.  
  105. # ----------------
  106. #     find the paths to the postgres, pg_version, and pg_id programs
  107. # ----------------
  108. if (test "$debug" -eq 1)
  109. then
  110.     echo "$CMDNAME: looking for postgres in:"
  111.     echo "$CMDNAME:   $PGBIN"
  112.     echo "$CMDNAME:   $TREE"
  113. fi
  114.  
  115. if (test -f $PGBIN/postgres)
  116. then
  117.     BACKEND=$PGBIN/postgres
  118.     PG_VERSION=$PGBIN/pg_version
  119.     PG_ID=$PGBIN/pg_id
  120.     if (test "$debug" -eq 1)
  121.     then
  122.     echo "$CMDNAME: found $BACKEND"
  123.     fi
  124. elif (test -f $TREE/*/support/postgres)
  125. then
  126.     BACKEND=$TREE/*/support/postgres
  127.     PG_VERSION=$TREE/*/support/pg_version
  128.     PG_ID=$TREE/*/support/pg_id
  129.     if (test "$debug" -eq 1)
  130.     then
  131.     echo "$CMDNAME: found $BACKEND"
  132.     fi
  133. else
  134.     echo "$CMDNAME: could not find postgres program"
  135.     echo "$CMDNAME: set POSTGRESHOME to the proper directory and rerun."
  136.     exit 1
  137. fi
  138.  
  139. # ----------------
  140. #     find the template files
  141. # ----------------
  142. if (test "$debug" -eq 1)
  143. then
  144.     echo "$CMDNAME: looking for template files..."
  145. fi
  146. if (test -f $PGTEMP/local1_template1.bki)
  147. then
  148.     TEMPLATE=$PGTEMP/local1_template1.bki
  149.     GLOBAL=$PGTEMP/global1.bki
  150. else
  151.     echo "$CMDNAME: warning: Make install has not been run"
  152.     TEMPLATE=`echo $TREE/*/support/local.bki`
  153.     GLOBAL=`echo $TREE/*/support/dbdb.bki`
  154.  
  155.     if (test ! -f $TEMPLATE)
  156.     then
  157.         echo "$CMDNAME: could not find template files"
  158.         echo "$CMDNAME: set POSTGRESTEMP to the proper directory and rerun."
  159.         exit 1
  160.     fi
  161. fi
  162. if (test "$debug" -eq 1)
  163. then
  164.     echo "$CMDNAME: using $TEMPLATE"
  165.     echo "$CMDNAME: using $GLOBAL"
  166. fi
  167.  
  168. #
  169. # Figure out who I am...
  170. #
  171.  
  172. UID=`$PG_ID`
  173.  
  174. # ----------------
  175. #     create the template database if necessary
  176. # ----------------
  177.  
  178. if (test ! -d $PG/data)
  179. then
  180.     for pg in $PGS
  181.     do
  182.        mkdir $pg/data $pg/data/base $pg/data/base/template1
  183.     done
  184.     if (test "$debug" -eq 1)
  185.     then
  186.     echo "$CMDNAME: creating SHARED relations in $PG/data"
  187.     echo "$CMDNAME: creating template database in $PG/data/base/template1"
  188.     fi
  189.  
  190.     $BACKEND $BACKENDARGS template1 < $TEMPLATE 
  191.  
  192.     if (test $? -ne 0)
  193.     then
  194.         echo "$CMDNAME: could not create template database"
  195.         if (test $noclean -eq 0)
  196.         then
  197.         echo "$CMDNAME: cleaning up."
  198.         for pg in $PGS
  199.         do
  200.                 rm -rf $pg/data
  201.         done
  202.         else
  203.         echo "$CMDNAME: cleanup not done (noclean mode set)."
  204.         fi
  205.     exit 1;
  206.     fi
  207.  
  208.     $PG_VERSION $PG/data/base/template1
  209.  
  210.     #
  211.     # Add the template database to pg_database
  212.     #
  213.  
  214.     echo "open pg_database" > /tmp/create.$$
  215.     echo "insert (template1 $UID template1)" >> /tmp/create.$$
  216.     echo "show" >> /tmp/create.$$
  217.     echo "close pg_database" >> /tmp/create.$$
  218.  
  219.     $BACKEND $BACKENDARGS template1 < $GLOBAL 
  220.  
  221.     if (test $? -ne 0)
  222.     then
  223.         echo "$CMDNAME: could create shared relations"
  224.         if (test $noclean -eq 0)
  225.         then
  226.         echo "$CMDNAME: cleaning up."
  227.         for pg in $PGS
  228.         do
  229.                 rm -rf $pg/data
  230.         done
  231.         else
  232.         echo "$CMDNAME: cleanup not done (noclean mode set)."
  233.         fi
  234.     exit 1;
  235.     fi
  236.  
  237.     $PG_VERSION $PG/data
  238.  
  239.     $BACKEND $BACKENDARGS template1 < /tmp/create.$$ 
  240.  
  241.     if (test $? -ne 0)
  242.     then
  243.         echo "$CMDNAME: could not log template database"
  244.         if (test $noclean -eq 0)
  245.         then
  246.         echo "$CMDNAME: cleaning up."
  247.         for pg in $PGS
  248.         do
  249.                 rm -rf $pg/data
  250.         done
  251.         else
  252.         echo "$CMDNAME: cleanup not done (noclean mode set)."
  253.         fi
  254.     exit 1;
  255.     fi
  256.  
  257.     rm -f /tmp/create.$$
  258. fi
  259.  
  260. # ----------------
  261. #     create the desired databases from the template database
  262. # ----------------
  263. for DBNAME in $FILES
  264. do
  265.     if (test -d $PG/data/base/$DBNAME)
  266.     then
  267.         echo "$CMDNAME: database $DBNAME already exists."
  268.     else
  269.         for pg in $PGS
  270.         do
  271.             mkdir $pg/data/base/$DBNAME
  272.         if (test "$debug" -eq 1)
  273.         then
  274.         echo "$CMDNAME: making directory $pg/data/base/$DBNAME"
  275.         fi
  276.             cp $pg/data/base/template1/* $pg/data/base/$DBNAME
  277.         done
  278.     
  279.         echo "open pg_database" > /tmp/create.$$
  280.         echo "insert ($DBNAME $UID $DBNAME)" >> /tmp/create.$$
  281.         echo "show" >> /tmp/create.$$
  282.         echo "close pg_database" >> /tmp/create.$$
  283.     if (test "$debug" -eq 1)
  284.     then
  285.         echo "$CMDNAME: creating database $DBNAME"
  286.     fi
  287.         $BACKEND $BACKENDARGS template1 < /tmp/create.$$ 
  288.     
  289.         if (test $? -ne 0)
  290.         then
  291.             echo "$CMDNAME: could not log new database"
  292.             if (test $noclean -eq 0)
  293.             then
  294.             echo "$CMDNAME: cleaning up."
  295.             for pg in $PGS
  296.             do
  297.                     rm -rf $pg/data/base/$DBNAME
  298.             done
  299.             else
  300.             echo "$CMDNAME: cleanup not done (noclean mode set)."
  301.             fi
  302.         exit 1;
  303.         fi
  304.  
  305.         rm -f /tmp/create.$$
  306.     fi
  307. done
  308.